package com.abiao.demo.controller;

import com.abiao.demo.domain.User;
import com.abiao.demo.service.UserService;
import com.abiao.demo.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: demo
 * @description: 用户
 * @author: luobiao
 * @date: 2022-01-24 16:19
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/insert")
    public Object insert(@RequestBody User user){
        return userService.insertUser(user);
    }

    @PostMapping("/update")
    public Object update(@RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody User user){
        return userService.deleteUser(user);
    }

    @GetMapping("/getUserByName")
    public Object getUserByName(@RequestParam String userName){
        return userService.findUserByName(userName);
    }

    @GetMapping("/getUserById/{id}")
    public Object getUserById(@PathVariable("id") Long id){
        return userService.getUserById(id);
    }

    /**
     * 用户分页列表
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
//    @GetMapping("/page")
//    public Object page(@RequestParam int pageNum,@RequestParam int pageSize){
//        PageHelper.startPage(pageNum, pageSize);
//        List<User> users = userService.getUserList();
//        PageInfo<User> pageInfos = new PageInfo<>(users);
//    }


    /**
     * 用户分页列表
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    @GetMapping("/page")
    public Object page(@RequestParam int pageNum,@RequestParam int pageSize){
        PageInfo<UserVO> pageInfos = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> list());
        return pageInfos;
    }

    /**
     * 用户列表
     * @return 用户列表
     */
    @GetMapping("/list")
    public Object list(){
        return userService.getUserList();
    }

}
